package fzdy.controller;

import fzdy.controller.dto.DeviceMalfunctionDTO;
import fzdy.controller.dto.PageDTO;
import fzdy.controller.dto.ReturnDTO;
import fzdy.core.vo.Page;
import fzdy.entity.DeviceMalfunction;
import fzdy.service.IDeviceMalfunctionService;
import fzdy.tools.TimeStampTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/9 0009.
 */
@Controller
@RequestMapping("/sysmanage/malfunction")
public class DeviceMalfunctionController {

    @Autowired
    private IDeviceMalfunctionService deviceMalfunctionService;

    @RequestMapping(value = "/malfunctionList")
    @ResponseBody
    public ReturnDTO malfunctionList(@RequestBody PageDTO pageDTO){
        Page<DeviceMalfunction> page=new Page<>();
        page.setPageSize(pageDTO.getPage());
        page.setPageSize(pageDTO.getRows());
        deviceMalfunctionService.getPageByExampleEq(new DeviceMalfunction(), page);
        List<DeviceMalfunctionDTO> dtos= DeviceMalfunctionPOJOtoDTO(page.getList()) ;
        return new ReturnDTO(dtos,page.getTotalRow().intValue());
    }


    private List<DeviceMalfunctionDTO> DeviceMalfunctionPOJOtoDTO(List<DeviceMalfunction> list ){
        List<DeviceMalfunctionDTO> dtos=new ArrayList<>();
        String pattern="yyyy-MM-dd HH:mm:ss";
        for (DeviceMalfunction mal:list) {
            DeviceMalfunctionDTO dto=new DeviceMalfunctionDTO();
            dto.setItemid(mal.getId());
            dto.setDeivceNum(mal.getDeviceSn());
            dto.setDeivceName(mal.getDeviceInfo().getDeviceName());
            dto.setDeivceType(mal.getDeviceInfo().getDeviceType().getDeviceType());
            dto.setInstallPosition(mal.getDeviceInfo().getLocation());
            dto.setProprietorUnit(mal.getDeviceInfo().getProprietorUnit().getProprietorName());
            dto.setCallThePoliceTime(TimeStampTool.timeStampToString(pattern,mal.getDeviceData().getReceivedTime()));
            dto.setFaultTime(TimeStampTool.timeStampToString(pattern,mal.getMalfunctionTime()));
            dto.setRegRepairTime(TimeStampTool.timeStampToString(pattern,mal.getRegRepairTime()));
            dto.setRepairSituation(mal.getRepairSituation());
            dto.setRestorer(mal.getAfterSaleServiceStaff().getStaffName());
            dto.setRepairTime(TimeStampTool.timeStampToString(pattern,mal.getRegRepairTime()));
            dtos.add(dto);
        }
        return dtos;

    }
}
